<template>
  <div>
    <div class="app-container">
      <h-table-list
        border
        type="local"
        :page-sizes="[10, 20]"
        :data="supply.records"
        @size-change="handleSizeChange"
        :columns="columns"
        :paginationInfo="paginationInfo"
        :form-options="formOptions"
      >
        <ListOpearteButton />
        <template slot="preview-column" slot-scope="scope">
          <p>
            <i
              v-if="scope.row.state == 0"
              class="iconfont deviceicon-dian-0"
            ></i>
            <i
              v-if="scope.row.state == 1"
              class="iconfont deviceicon-dian-1"
            ></i>
            <i
              v-if="scope.row.state == 2"
              class="iconfont deviceicon-dian-2"
            ></i>
            {{ scope.row.state | formatState }}
          </p>
        </template>
        <template slot="operate-column" slot-scope="scope">
          <tableButton :row="scope.row" />
        </template>
      </h-table-list>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import waves from "@/directive/waves"; // waves directive
import { parseTime } from "@/utils";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import ListOpearteButton from "@/components/AllButton/button2";
import tableButton from "@/components/AllButton/button3";
import data from "./const";

export default {
  name: "supply",
  components: {
    DownloadIdentify,
    IdentifyContent,
    Pagination,
    tableButton,
    ListOpearteButton,
  },
  directives: { waves },
  filters: {
    typeFilter(type) {
      return calendarTypeKeyValue[type];
    },
  },
  data() {
    return {
      dialogFormVisible: false,
      loadList: false,
      paginationInfo: {
        sizeChange: (e) => {
          console.log(e);
        },
        currentChange: (e) => {
          this.$store.dispatch("supply/setListSearchParams", {
            current: e,
          });
          this.getList(
            Object.assign({}, this.$store.state.supply.listSearchParams)
          );
        },
        current: 1,
        total: 10,
      },
      formOptions: {
        inline: true,
        submitBtnText: "查询",
        showResetBtn: true,
        searchParamsHandler: (params) => {
          console.log("查询");
          this.getList(params);
        },
        changevalue: (params) => {
          //   const forms=this.formOptions.forms.map(e=>{
          //      const key=e.prop
          //      e.defaultValue=params[key]
          //     return e
          //    })
          //  console.log(forms)
          console.log(params);
          this.$store.dispatch("supply/setListSearchParams", params);
        },
        resetBtnCallback: () => {
          this.$store.dispatch("supply/setListSearchParams", {});
          this.getList(this.$store.state.supply.listSearchParams);
        },
        forms: [
          { prop: "repNumber", label: "备件名称" },

          { prop: "deviceNumber", label: "备件编号" },
          { prop: "deviceNumber", label: "规格型号" },
          { prop: "deviceNumber", label: "类别" },
          {
            prop: "useDepartmentId",
            label: "归属部门",
            itemType: "select",
            options: [
              { value: "", label: "部门1" },
              { value: 0, label: "部门2" },
              { value: 1, label: "部门3" },
            ],
          },

          {
            prop: "status",
            label: "通用/专用",
            itemType: "select",
            options: [
              { value: 0, label: "通用" },
              { value: 1, label: "专用" },
            ],
          },
        ],
      },
      columns: [
        { type: "index", prop: "repNumber", label: "序号" },

        {
          prop: "deviceName",
          label: "备件名称",
          width: 100,
          // render: (row) => {
          //   const { deviceInfo } = row;
          //   return deviceInfo.name ? deviceInfo.name :"";
          // },
        },
        {
          prop: "deviceNumber",
          label: "备件编号",
          width: 100,
          // render: (row) => {
          //   const { deviceInfo } = row;
          //   return deviceInfo.deviceNumber ? deviceInfo.deviceNumber :"";
          // },
        },
        // { prop: "departmentName", label: "使用部门", minWidth: 180, render: (row) => {
        //     const { departmentInfo } = row;
        //     return departmentInfo.name ? departmentInfo.name :"";
        //   },},
        { prop: "departmentName", label: "规格型号", minWidth: 180 },
        { prop: "membName", label: "归属部门", minWidth: 180 },
        // {
        //   prop: "itemsSum",
        //   label: "点检项",
        //   slotName: "items-sum",
        //   minWidth: 180,
        // },
        { prop: "reportTime", label: "库存", minWidth: 180 },
        {
          prop: "status",
          label: "单价",
          minWidth: 180,
          slotName: "preview-column",
        },
        {
          prop: "operate",
          label: "操作",
          minWidth: 240,
          slotName: "operate-column",
          fixed: "right",
        },
      ],
      column2s: [
        { prop: "id", label: "序号", width: 140 },
        { prop: "name", label: "计划名称", minWidth: 180 },

        {
          prop: "deviceName",
          label: "设备名称",
          width: 100,
          // render: (row) => {
          //   const { deviceInfo } = row;
          //   return deviceInfo.name ? deviceInfo.name :"";
          // },
        },
        {
          prop: "deviceNumber",
          label: "设备名称",
          width: 100,
          // render: (row) => {
          //   const { deviceInfo } = row;
          //   return deviceInfo.deviceNumber ? deviceInfo.deviceNumber :"";
          // },
        },
        // { prop: "departmentName", label: "使用部门", minWidth: 180, render: (row) => {
        //     const { departmentInfo } = row;
        //     return departmentInfo.name ? departmentInfo.name :"";
        //   },},
        { prop: "departmentName", label: "使用部门", minWidth: 180 },
        { prop: "membName", label: "点检员", minWidth: 180 },
      ],
      tableData: [],
    };
  },

  watch: {
    formChange: () => {},
  },
  computed: {
    ...mapState(["supply", "app"]),
  },
  created() {
    //初始获取几个基本数据,分别为操作状态,按钮权限等
    (this.formOptions.forms = this.formOptions.forms.map((e) => {
      const key = e.prop;
      const data = this.$store.state.supply.listSearchParams;
      e.defaultValue = data[key] || "";
      return e;
    })),
      //this.submitHandler=this.searchWithParams
      this.$store
        .dispatch(this.$store_url_button, {
          add: this.add,
          export: this.export,
          delete: this.delete,
          edit: this.edit,
          detail: this.detail,
          using: this.using,
          useless: this.useless,
          downloadTemplete: this.downloadTemplete,
          downloadIdentify: this.downloadIdentify,
        })
        .then(async () => {
          //await this.$store.dispatch("app/getCodeList", []); //code转码
          this.getList(); // 表格数据
        });

    // this.listQuery=this.$store.state.basicEquipment.listSearchParams||{}  //操作状态
  },
  methods: {
    //自定义该业务模块下的按钮方法
    using(row) {
      console.log("using", row);
    },
    useless(row) {
      console.log("useless", row);
    },
    add(row) {
      console.log(row);
      this.$router.push("/device-basic/component/edit/index");
    },
    edit(row) {
      this.$router.push("/device-basic/component/edit/index");
      console.log("edit");
    },
    downloadIdentify(row) {
      console.log("下载设备标识卡");
      this.dialogFormVisible = true;
    },
    async downloadTemplete(ids) {
      console.log("下载导出模板");
      const download = await this.$store.dispatch("supply/downloadTemplete");
      //window.open(download.)
      console.log(download);
    },
    delete(row) {
      console.log(row);
      this.$confirm(`确认删除？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$store
            .dispatch("supply/deleteDeviceInfo", row.id)
            .then((response) => {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: "删除失败",
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    update() {},
    detail(row) {
      console.log(row);

      localStorage.setItem("/device-basic/component/detail/index", row.id);
      this.$router.push("/device-basic/component/detail/index");
    },
    import() {},
    export() {
      console.log("导出");
      this.$store.dispatch("supply/exportFile");
    },
    //弹窗的关闭
    cancel() {
      console.log("关闭弹窗");
      this.dialogFormVisible = false;
    },
    //更新弹窗数据
    updateDialog() {
      console.log("更新并关闭弹窗");
      this.dialogFormVisible = false;
    },
    //查询按钮
    searchWithParams() {
      console.log("查询");
    },
    async getList(args) {
      // this.listLoading = true
      console.log(args);
      await this.$store.dispatch("supply/requestDeviceList", args);
      this.paginationInfo.total = this.$store.state.supply.total;
    },
    handleFilter(args) {
      this.listQuery.page = 1;
      this.getList(args);
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: "操作成功",
        type: "success",
      });
      row.status = status;
    },
    sortChange(data) {
      const { prop, order } = data;
      if (prop === "id") {
        this.sortByID(order);
      }
    },
    sortByID(order) {
      if (order === "ascending") {
        this.listQuery.sort = "+id";
      } else {
        this.listQuery.sort = "-id";
      }
      this.handleFilter();
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: "",
        timestamp: new Date(),
        title: "",
        status: "published",
        type: "",
      };
    },
    handleCreate() {
      // this.resetTemp();
      // this.dialogStatus = "create";
      // this.dialogFormVisible = true;
      // this.$nextTick(() => {
      //   this.$refs["dataForm"].clearValidate();
      // });
      this.dialogFormVisible = true;
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row); // copy obj
      this.temp.timestamp = new Date(this.temp.timestamp);
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    handleDelete(row) {
      this.$notify({
        title: "成功",
        message: "删除成功",
        type: "success",
        duration: 2000,
      });
      const index = this.list.indexOf(row);
      this.list.splice(index, 1);
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map((v) =>
        filterVal.map((j) => {
          if (j === "timestamp") {
            return parseTime(v[j]);
          } else {
            return v[j];
          }
        })
      );
    },
  },
};
</script>

